<!DOCTYPE html>
<html>

	<head>
		<meta charset="UTF-8">
		<meta name="viewport" id="vp" content="width=device-width, initial-scale=1.0,minimum-scale=1.0,maximum-scale=1.0,user-scalable=no" />
		<title>手机号邀请</title>
		<link href="../../../css/style.css" rel="stylesheet" type="text/css">
		<link href="../../../css/mui.css" rel="stylesheet" type="text/css">
		<link href="../../../css/mui.picker.min.css" rel="stylesheet" />
		<style>
			input {
				width: 70%!important;
				border: none!important;
			}
			
			.tel {
				background: #fff;
				height: 45px;
				line-height: 45px;
				border: solid 1px #dedede;
				border-left: none;
				border-right: none;
			}
			
			.tel span {
				text-align: center;
				width: 30%;
				display: inline-block;
				border-left: solid 1px #dedede;
			}
			
			.btnAdd {
				width: 80%;
				margin: 0 auto;
				border: none;
				border-radius: 10px;
				background: #1bb7fd;
				color: #fff;
				display: block;
				margin-top: 20px;
			}
			
			.update {
				font-style: normal;
				color: #999999;
				margin-right: -25px;
				font-size: 15px
			}
			.mui-table-view-chevron .mui-table-view-cell{
				padding-right: 40px;
			}
		</style>
	</head>

	<body>
		<div id="telinvite">
			<ul class="mui-table-view  mui-table-view-chevron" style="margin-top: 0px;">
				<li class="mui-table-view-cell" id='showUserPicker'>
					<a class=""><span>与宝宝的关系</span><span class="fr" id="relation"></span></a>
				</li>
			</ul>
			<div class="tel">
				<input type="tel" class="fl" id="tel" placeholder="请输入手机号码" />

			</div>
			<button class="btnAdd" type="button" onclick="shareHref()">添加</button>
			<!-- 主页面标题 -->

			<input id="sharehref" type="url" value="http://www.baidu.com/" hidden="hidden" />

			<input id="sharehrefTitle" type="text" hidden="hidden" value="邀请你加入童伴App" />

			<input id="sharehrefDes" type="text" hidden="hidden" value="邀请你加入童伴App" /></br>

		</div>
		<script src="../../../js/mui.min.js "></script>
		<script src="../../../js/mui.view.js "></script>
		<script src="../../../js/mui.picker.js"></script>
		<script src="../../../js/mui.picker.min.js"></script>
		<script src="../../../js/mui.poppicker.js"></script>
		<script type="text/javascript" src="../../../js/jquery-1.8.3.min.js"></script>
		<script src="../../../js/app.js"></script>
		<script src="../../../js/token.js"></script>
		<script src="../../../js/vue.min.js"></script>
		<script> 
			function checkMobile() {
					var sMobile = document.getElementById('tel').value;
				    var myreg = /^[1][3,4,5,7,8][0-9]{9}$/;
				    if (!myreg.test(sMobile)) {
				    	alert("不是完整的11位手机号或者正确的手机号前七位");
						document.getElementById('tel').focus();
				      return false;
				    } else {
				      return true;
				    }
			}
			var relationship='';
			var relationshipisea="";
			mui.plusReady(function() {
				var self = plus.webview.currentWebview();
				relationship = self.relationship;//wenzi
				relationshipisea=self.relationshipisea//yingwen
				document.getElementById('relation').innerHTML = relationship;

				var state = app.getState();
				uid = state.userid;
				memberToken= state.memberToken;
				childid = state.childid;
				mui.post(Apiurl+'index/member_info', {
					uid: uid,
					memberToken: memberToken,
					child_id:childid
				}, function(data) {

					telinvite.items = data
				}, 'json')
			})
			var telinvite = new Vue({
				el: '#telinvite',
				data: {
					items: []
				},
				methods: {

				}
			});
			var shares = null;
			var Intent = null,
				File = null,
				Uri = null,
				main = null;
			// H5 plus事件处理
			function plusReady() {
				updateSerivces();
				if(plus.os.name == "Android") {
					main = plus.android.runtimeMainActivity();
					Intent = plus.android.importClass("android.content.Intent");
					File = plus.android.importClass("java.io.File");
					Uri = plus.android.importClass("android.net.Uri");
				}
			}
			if(window.plus) {
				plusReady();
			} else {
				document.addEventListener("plusready", plusReady, false);
			}

			/**
			 * 
			 * 更新分享服务
			 */
			function updateSerivces() {
				plus.share.getServices(function(s) {
					shares = {};
					for(var i in s) {
						var t = s[i];
						shares[t.id] = t;
					}
				}, function(e) {
					alert("获取分享服务列表失败：" + e.message);
				});
			}

			/**
			 * 分享操作
			 * @param {JSON} sb 分享操作对象s.s为分享通道对象(plus.share.ShareService)
			 * @param {Boolean} bh 是否分享链接
			 */
			function shareAction(sb, bh) {
				if(!sb || !sb.s) {
					alert("无效的分享服务！");
					return;
				}

				var msg = {
					content: sharehrefDes.value,
					extra: {
						scene: sb.x
					}
				};
				if(bh) {
					msg.href = sharehref.value;
					if(sharehrefTitle && sharehrefTitle.value != "") {
						msg.title = sharehrefTitle.value;
					}
					if(sharehrefDes && sharehrefDes.value != "") {
						msg.content = sharehrefDes.value;
					}
					msg.thumbs = ["_www/logo.png"];
					msg.pictures = ["_www/logo.png"];
				} else {
					if(pic && pic.realUrl) {
						msg.pictures = [pic.realUrl];
					}
				}
				// 发送分享
				if(sb.s.authenticated) {
					console.log("---已授权---");
					shareMessage(msg, sb.s);
				} else {
					console.log("---未授权---");
					sb.s.authorize(function() {
						shareMessage(msg, sb.s);
					}, function(e) {
						console.log("认证授权失败：" + e.code + " - " + e.message);

					});
				}
			}
			/**
			 * 发送分享消息
			 * @param {JSON} msg
			 * @param {plus.share.ShareService} s
			 */
			
			function shareMessage(msg, s) {
				var tela = document.getElementById('tel').value;
				// alert(JSON.stringify(msg));
				s.send(msg, function() {
					plus.nativeUI.toast('恭喜您获得'+20+"积分", {
												verticalAlign: 'middle'
											});
											mui.fire(plus.webview.getWebviewById('invite') , "refreshinvite")
					//alert(uid+'-'+memberToken+'-'+relationshipisea+'-'+tela)
					
					console.log("分享到\"" + s.description + "\"成功！ ");

				}, function(e) {
					console.log("分享到\"" + s.description + "\"失败: " + JSON.stringify(e));

				});
			}
			// 分析链接
			function shareHref() {
				if(checkMobile()) {
					var relationshipchange=document.getElementById('relation').innerHTML;
				  
					if(relationshipchange == "爸爸") { 
						relationshipisea = 6;
					} else if(relationshipchange == "妈妈") {
						relationshipisea = 1;
					} else if(relationshipchange == "爷爷") {
						relationshipisea = 2;
					} else if(relationshipchange == "奶奶") {
						relationshipisea = 3;
					} else if(relationshipchange == "姥姥") {
						relationshipisea = 4;
					} else if(relationshipchange == "姥爷") { 
						relationshipisea = 5;
					} 
					var tela = document.getElementById('tel').value;
					
					document.getElementById('sharehref').value = "http://tongban.dqbenxin.com/invitepublic/?uid=" + uid + "&relationship=" + relationshipisea + '&tel=' + tela;
					//alert(relationshipisea)
					mui.post(Apiurl+'index/yaoqing', {
						uid:uid,
						memberToken:memberToken,
						relationship: relationshipisea,
						tel:tela,
						child_id:childid
					}, function(data) {
						if(data.status==201){
							mui.toast(data.errmsg);
						}else if(data.status==202){
							mui.toast(data.errmsg);
						}else{
					
					
					var shareBts = [];
					// 更新分享列表
					var ss = shares['weixin'];

					ss && ss.nativeClient && (shareBts.push({
						title: '微信好友',
						s: ss,
						x: 'WXSceneSession'
					}));
					/*ss = shares['qq']; 
					ss && ss.nativeClient && shareBts.push({
						title: 'QQ',
						s: ss
					});*/
					// 弹出分享列表
					shareBts.length > 0 ? plus.nativeUI.actionSheet({title: '分享链接',cancel: '取消',buttons: shareBts}, function(e) {
						(e.index > 0) && shareAction(shareBts[e.index - 1], true)}
					) : plus.nativeUI.alert('当前环境无法支持分享链接操作!')
						
						}
					} , 'json')
				}
				
				}
		</script>
	</body>

</html>